package com.njlime.sys.controller;

import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.PageBean;
import com.njlime.core.web.validation.annotation.support.ValidateService;
import com.njlime.sys.entities.Role;
import com.njlime.sys.service.MenuService;
import com.njlime.sys.service.RoleService;
import com.njlime.sys.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Dean on 2017-7-25.
 */
@Controller
@RequestMapping(value = "sys/role")
public class RoleController extends BaseController{
    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;
    @Resource
    private MenuService menuService;

    //-------------------控制器--------------------

    /**
     * 跳转热点管理页面
     * @return
     */
    @RequestMapping(value = "roleManage")
    public ModelAndView roleManage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/terrace/role/index");
        return modelAndView;
    }

    //------------------接口-----------------


    /**
     * 分页获取角色列表
     * @param request
     * @param role
     * @param pageBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "roleList", method = RequestMethod.GET)
    public String roleList(HttpServletRequest request, Role role, PageBean pageBean) {
        try {
            if (pageBean == null) {
                pageBean = new PageBean();
            }
            if(role == null){
                role = new Role();
            }
            return toJsonListData(roleService.list(pageBean, role));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 角色新增
     * @param request
     * @param role
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public String add(HttpServletRequest request, Role role) {
        try {
            /*Unit rootUnit = getLoginUser().getRootUnit();
            if(rootUnit == null){
                role.setUnit_id(GlobalValue.SYS_UNIT_ID);
            }else{
                role.setUnit_id(rootUnit.getId());
            }*/
            ValidateService.valid(role);
            roleService.addRole(role);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 角色信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "roleInfo", method = RequestMethod.GET)
    @ResponseBody
    public String roleInfo(HttpServletRequest request, Integer id) {
        try {
            return toJsonSuccessData(roleService.get(id));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 角色编辑
     * @param request
     * @param role
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(HttpServletRequest request, Role role) {
        try {
            roleService.editRole(role);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 删除角色
     * @param request
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(HttpServletRequest request,String ids) {
        try {
            String[] idArray = ids.split(",");
            roleService.deleteRole(idArray);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 根据角色获取用户
     * @param request
     * @param role_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getUserByRole", method = RequestMethod.GET)
    public String getUserByRole(HttpServletRequest request,Integer role_id){
        try {
            return toJsonListData(userService.getUserByRole(role_id));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 设置角色用户
     * @param request
     * @param role_id
     * @param user_ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "setRoleUser", method = RequestMethod.POST)
    public String setRoleUser(HttpServletRequest request,Integer role_id,String user_ids){
        try {
            roleService.setRoleUser(role_id,user_ids);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 根据角色获取菜单id字符串
     * @param request
     * @param role_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMenuByRole", method = RequestMethod.GET)
    public String getMenuByRole(HttpServletRequest request,Integer role_id){
        try {
            //return toJsonListData(menuService.getMenuByRole(role_id));
            /*Role role = new Role();
            role.setId(role_id);
            role.setMenu_ids(menuService.getMenuIdsByRole(role_id));*/
            return toJsonSuccessData(menuService.menuTree(role_id));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 设置角色菜单
     * @param request
     * @param role_id
     * @param menu_ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "setRoleMenu", method = RequestMethod.POST)
    public String setRoleMenu(HttpServletRequest request,Integer role_id,String menu_ids){
        try {
            roleService.setRoleMenu(role_id,menu_ids);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

}